﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NUnit.Framework;
using NHibernateSample.Domain.Entities;
using NHibernate;

namespace NHibernateSample.DAL.Test
{
    [TestFixture]
    public class QueryCriteriaAPIFixture
    {
        private NHibernateHelper _helper;
        private QueryCriteriaAPI _queryCriteriaAPI;

        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            _helper = new NHibernateHelper();
            HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();
        }

        [SetUp]
        public void Setup()
        {
            _queryCriteriaAPI = new QueryCriteriaAPI(_helper.GetSession());
         }

        #region 语法测试
        [Test]
        public void CreateCriteriaTest()
        {
            IList<Customer> customers = _queryCriteriaAPI.CreateCriteria();
            Assert.AreEqual(2, customers.Count);
        }

        [Test]
        public void NarrowingTest()
        {
            IList<Customer> customers = _queryCriteriaAPI.Narrowing();
            Assert.AreEqual(0, customers.Count);
        }
        [Test]
        public void OrderTest()
        {
            IList<Customer> customers = _queryCriteriaAPI.Order();
            Assert.AreEqual(1, customers.Count);
        }

        [Test]
        public void QueryTest()
        {
            IList<Customer> customers = _queryCriteriaAPI.Query();
            Assert.AreEqual(1, customers.Count);
        }

        [Test]
        public void UseQueryByExample_GetCustomerTest()
        {
            Customer customerSample = new Customer() { FirstName = "董", LastName = "董" };
            IList<Customer> customers = _queryCriteriaAPI.UseQueryByExample_GetCustomer(customerSample);
            Assert.AreEqual(1, customers.Count);
        }
        #endregion

        #region 实例测试
        [Test]
        public void CanUseCriteriaAPI_GetCustomersByFirstName()
        {
            IList<Customer> customers = _queryCriteriaAPI.UseCriteriaAPI_GetCustomersByFirstName("李");
            Assert.AreEqual(1, customers.Count);
        }

        [Test]
        public void CanUseCriteriaAPI_GetCustomerByFirstNameAndLastName()
        {
            IList<Customer> customers = _queryCriteriaAPI.UseCriteriaAPI_GetCustomersByFirstNameAndLastName("李", "四");
            Assert.AreEqual(1, customers.Count);
        }

        [Test]
        public void CanUseCriteriaAPI_CanGetCustomersWithIdGreaterThan()
        {
            IList<Customer> customers = _queryCriteriaAPI.UseCriteriaAPI_GetCutomersWithIdGreaterThan(1);
            foreach (Customer c in customers)
            {
                Assert.AreEqual(c.Id, 11);
                Assert.GreaterOrEqual(c.Id, 1);
            }
        }
        #endregion
    }
}
